from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models

from home.models import CourseCategory, NavCategory
from utils.models import BaseModel


class Teacher(BaseModel):
    ROLE_CHOICES = (
        (0, "讲师"),
        (1, "导师"),
        (2, "班主任")
    )
    name = models.CharField(max_length=255, verbose_name="讲师姓名")
    role = models.IntegerField(verbose_name="讲师角色", choices=ROLE_CHOICES, default=0)
    title = models.CharField(max_length=255, verbose_name="讲师职称")
    signature = models.CharField(max_length=255, verbose_name="讲师签名")
    avatar = models.CharField(max_length=255, verbose_name="讲师头像")
    brief = models.TextField(verbose_name="讲师描述")

    class Meta:
        db_table = "tb_teacher"
        verbose_name = "讲师"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class CourseTag(BaseModel):
    """课程标签"""
    name = models.CharField(max_length=20, verbose_name="标签名称", unique=True)

    class Meta:
        db_table = "tb_course_tag"
        verbose_name = "课程标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Course(BaseModel):
    COURSE_TYPE_CHOICES = (
        (2, "付费"),
        (1, "免费")
    )
    COURSE_STATUS_CHOICES = (
        (2, "预上线"),
        (1, "上线"),
        (0, "下线")
    )
    level_CHOICES = (
        (3, "资深"),
        (2, "高级"),
        (1, "中级"),
        (0, "初级")
    )

    """课程"""
    name = models.CharField(max_length=50, verbose_name="课程名称")
    brief = models.TextField(verbose_name="课程简介")
    teacher = models.ForeignKey(Teacher, on_delete=models.DO_NOTHING, verbose_name="课程讲师")
    students = models.IntegerField(verbose_name="课程学习人数")
    lessons = models.IntegerField(verbose_name="课程课时数")
    pub_lessons = models.IntegerField(verbose_name="课程已更新课时数")
    course_type = models.IntegerField(verbose_name="课程付费类型", choices=COURSE_TYPE_CHOICES, default=2)
    original_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="课程原价", default=0)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="课程现价", default=0)

    course_img = models.CharField(max_length=255, verbose_name="课程封面图")
    pub_date = models.DateField(verbose_name="课程发布时间", auto_now_add=True)

    status = models.IntegerField(verbose_name="课程状态", default=1, choices=COURSE_STATUS_CHOICES)

    level = models.IntegerField(verbose_name="课程难度", choices=level_CHOICES, default=1)

    categories = models.ManyToManyField(CourseCategory, verbose_name="课程分类", related_name="course_categories")

    attachment_path = models.CharField(max_length=255, verbose_name="课程课件路径", null=True, blank=True)
    description = RichTextUploadingField(verbose_name="课程描述", null=True, blank=True)
    outcomes = RichTextUploadingField(verbose_name="课程收获", null=True, blank=True)
    audience = RichTextUploadingField(verbose_name="适合人群", null=True, blank=True)

    class Meta:
        db_table = "tb_course"
        verbose_name = "课程"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class CourseChapter(BaseModel):
    """课程章节"""
    number = models.IntegerField(verbose_name="章节序号")
    title = models.CharField(max_length=50, verbose_name="章节标题")
    summary = RichTextUploadingField(verbose_name="章节描述", null=True, blank=True)
    pub_date = models.DateField(verbose_name="发布时间", auto_now_add=True)

    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="所属课程", related_name="chapters")

    class Meta:
        db_table = "tb_course_chapter"
        verbose_name = "课程章节"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.pk};{self.title};{self.number}"


class CourseLesson(BaseModel):
    """课程课时"""
    LESSON_TYPE = (
        (0, "PPT"),
        (1, "文档"),
        (2, "视频")
    )

    title = models.CharField(max_length=50, verbose_name="课时标题")
    orders = models.IntegerField(verbose_name="课时序号")
    lesson_type = models.IntegerField(verbose_name="课时类型", choices=LESSON_TYPE, default=1)
    link = models.CharField(max_length=255, verbose_name="课时链接", null=True, blank=True)
    duration = models.CharField(verbose_name="课时时长", max_length=100)

    pub_date = models.DateField(verbose_name="发布时间", auto_now_add=True)

    is_free_trail = models.BooleanField(verbose_name="是否免费试看", default=False)
    chapter = models.ForeignKey(CourseChapter, on_delete=models.CASCADE, verbose_name="所属章节",
                                related_name="lessons")

    class Meta:
        db_table = "tb_course_lesson"
        verbose_name = "课程课时"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class CourseNavRelation(BaseModel):
    """课程和导航分类关系"""
    """
    course字段定义了从CourseNavRelation到Course的外键关系，并设置了related_name="nav_relations"。
    这意味着你可以通过course.nav_relations.all()来获取与某个课程相关的所有导航关系记录。
    nav_category字段定义了从CourseNavRelation到NavCategory的外键关系，并设置了related_name="course_relations"。
    这意味着你可以通过nav_category.course_relations.all()来获取与某个导航分类相关的所有课程关系记录。
    """
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程", related_name="nav_relations")

    nav_category = models.ForeignKey(NavCategory, on_delete=models.CASCADE, verbose_name="导航分类",
                                     related_name="course_relations")

    class Meta:
        db_table = "tb_course_nav_relation"
        verbose_name = "课程和导航分类关系"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.course.name


class CourseTagRelation(BaseModel):
    """课程和标签关系"""

    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程", related_name="tag_relations")

    tag = models.ForeignKey(CourseTag, on_delete=models.CASCADE, verbose_name="标签", related_name="course_relations")

    class Meta:
        db_table = "tb_course_tag_relation"
        verbose_name = "课程和标签关系"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.course.name


class CourseExpire(BaseModel):
    course = models.ForeignKey('Course',on_delete=models.CASCADE,verbose_name="课程",related_name="course_expire")
    expire_time = models.IntegerField(verbose_name="课程有效期",help_text="单位:天,1表示永久有效")
    price= models.DecimalField(max_digits=10,decimal_places=2,verbose_name="课程价格",help_text="单位:元")


    class Meta:
        db_table = "tb_course_expire"
        verbose_name = "课程有效期"
        verbose_name_plural = verbose_name


